package com.example.controller;
import com.example.dao.GoodsRepository;
import com.example.doc.GoodsInfo;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Iterator;
import java.util.Map;


@RestController
public class GoodsController {

    @Autowired
    private GoodsRepository goodsRepository;
    @Autowired
    TransportClient client;


    /**
     * 添加索引
     * @return
     */
    @GetMapping("save")
    public String save(){

        GoodsInfo goodsInfo = new GoodsInfo(System.currentTimeMillis(),
                "这都是什么鬼"+System.currentTimeMillis(),"这都是什么鬼");
        goodsRepository.save(goodsInfo);
        return "success";
    }

    /**
     * 设置高亮
     */
    @GetMapping("hig")
    public String getHighlight(){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//构造搜索对象
        searchSourceBuilder.query(QueryBuilders.matchQuery("name",  "鬼"));//设置查询条件
        //设置高亮
        String preTags = "<span style='color:red'>";
        String postTags = "</span>";
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags(preTags);//设置前缀
        highlightBuilder.postTags(postTags);//设置后缀
        highlightBuilder.field("name");//设置高亮字段
        searchSourceBuilder.highlighter(highlightBuilder);//设置高亮信息
        SearchRequest searchRequest = new SearchRequest("sgoods");//创建查询请求对象
        searchRequest.types("student");//设置type
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest)
                .actionGet();//执行
        SearchHits hits = searchResponse.getHits();
        SearchHit[] searchHits = hits.getHits();
        for (SearchHit searchHit : searchHits) {
            Map<String, HighlightField> map = searchHit.getHighlightFields();
            System.out.println(map.toString());
        }
        return  "";
    }
    /**
     * 进行检索
     * @return
     */
    @GetMapping("get")
    public String getGoodsInfo(){
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchQuery("name",  "鬼"))
                .build();
        Iterable<GoodsInfo>  goodsInfos= goodsRepository.search(searchQuery);

        for (Iterator iter = goodsInfos.iterator(); iter.hasNext();) {
            GoodsInfo str = (GoodsInfo)iter.next();
            System.out.println(str.getName());
        }

   /* .withQuery(QueryBuilders.matchQuery("name",  "鬼"))*/
        System.out.println(searchQuery.getQuery());
        return  "success";
    }

    /**
     * 删除
     * @return
     */
    @GetMapping("delete")
    public String delete(){
        goodsRepository.deleteById(Long.parseLong("1541641603146") );
      return  "success"  ;
    }

    /**
     *
     * @return
     */
    public String getHighLigth(){
        return "success";
    }
}
